<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Service extends Base
{
    protected $id = 'maintenance_record_id';
    protected $table = 'maintenance_record';
    protected $join = ['car c','c.car_id = m.car_id'];
    protected $field = 'm.*,c.number';

    //下载excel表格
    public function outexcel()
    {

        $P = input('param.');
        $where = [];

        $P['car_id'] && $where['car_id'] = ['=', $P['car_id']];
        $P['start_date'] && $where['start_date'] = ['>=', $P['start_date']];
        $P['end_date'] && $where['end_date'] = ['<=', $P['end_date']];

        $maintenance_record_return = Db::name('maintenance_record')->where($where)->whereNull('delete_time')->field('car_id,numbers,service_type,price,leader,service_company,start_date,end_date,service_linkman,service_phone,km,address,remind')->order('start_date desc')->select();
        $car_return = Db::name('car')->where('')->field('car_id,number')->select();

        if ($maintenance_record_return) {
            echo "<table border='1'>";
            //标题
            $title = array("车牌号", "维保单号", "维保类型", "维保金额", "送维/送保人", "维保公司", "开始时间", "结束时间", "维保公司联系人", "维保公司联系电话", "公里数", "地点", "备注说明");
            //文件名
            $filename = '维保管理导出报表';
            header("Content-type:application/octet-stream");
            header("Accept-Ranges:bytes");
            header("Content-type:application/vnd.ms-excel");
            header("Content-Disposition:attachment;filename=" . $filename . ".xls");
            header("Pragma: no-cache");
            header("Expires: 0");
            if (!empty($title)) {
                foreach ($title as $k => $v) {
                    $title[$k] = iconv("UTF-8", "GB2312", "<td>" . $title[$k] . "</td>");
                    echo $title[$k];
                }
                echo "</tr>";
            }
            if (!empty($maintenance_record_return)) {
                foreach ($maintenance_record_return as $key => $val) {

                    if ($maintenance_record_return[$key]['service_type'] == '1') {
                        $maintenance_record_return[$key]['service_type'] = "维修";
                    } else {
                        $maintenance_record_return[$key]['service_type'] = "保养";
                    }

                    foreach ($car_return as $key2 => $val2) {
                        if ($car_return[$key2]['car_id'] == $maintenance_record_return[$key]['car_id']) {
                            $maintenance_record_return[$key]['car_id'] = $car_return[$key2]['number'];
                        }
                    }

                    foreach ($val as $ck => $cv) {
                        $maintenance_record_return[$key][$ck] = iconv("UTF-8", "GB2312", "<td style=\"vnd.ms-excel.numberformat:@\">" . $maintenance_record_return[$key][$ck] . "</td>");
                        echo $maintenance_record_return[$key][$ck];
                    }
                    echo "</tr>";
                }
            }
        } else {
            $this->error('无数据');
        }
    }
}
